﻿using UnityEngine;
using System.Collections;

public class CamTouchReceiver : MonoBehaviour {

	public bool AllowMove;
	public float MoveSpeed;
	
	public bool AllowFastMove;
	public float FastMoveSpeed;
	public KeyCode FastMoveKeyCode1;
	public KeyCode FastMoveKeyCode2;
	
	public bool AllowRotate;
	public float RotateSpeed;
	
	public bool AllowZoom;
	public float ZoomSpeed;
	
	public bool AllowTilt;
	public float TiltSpeed;
	
	public KeyCode ResetKey;
	public bool IncludePositionOnReset;
	
	public bool MovementBreaksFollow;

	
	public bool RotateUsesInputAxis = false;

	
	public bool ZoomUsesInputAxis = false;

	
	//
	
	private LegionTouchCam _cam;


	public void Reset()
	{
		AllowMove = true;
		MoveSpeed = 20f;
		
		AllowFastMove = true;
		FastMoveSpeed = 40f;
		
		AllowRotate = true;
		RotateSpeed = 70f;
		
		AllowZoom = true;
		ZoomSpeed = 10f;
		
		AllowTilt = true;
		TiltSpeed = 70f;

		IncludePositionOnReset = false;
		
		MovementBreaksFollow = true;
	}

	protected void Start()
	{
		_cam = gameObject.GetComponent<LegionTouchCam>();
	}

	void OnEnable()
	{
		//EasyTouch.On_TouchStart += On_TouchStart;
		EasyTouch.On_SwipeStart += On_SwipeStart;
		EasyTouch.On_Swipe += On_Swipe;
		EasyTouch.On_SwipeEnd += On_SwipeEnd;
		EasyTouch.On_PinchIn += On_PinchIn;
		EasyTouch.On_PinchOut += On_PinchOut;
		EasyTouch.On_PinchEnd += On_PinchEnd;
		EasyTouch.On_PinchEnd += On_Twist;
		EasyTouch.On_PinchEnd += On_TwistEnd;
	}

	void DisableTouch()
	{
		//EasyTouch.On_TouchStart -= On_TouchStart;
		EasyTouch.On_SwipeStart -= On_SwipeStart;
		EasyTouch.On_Swipe -= On_Swipe;
		EasyTouch.On_SwipeEnd -= On_SwipeEnd;
		EasyTouch.On_PinchIn -= On_PinchIn;
		EasyTouch.On_PinchOut -= On_PinchOut;
		EasyTouch.On_PinchEnd -= On_PinchEnd;
		EasyTouch.On_PinchEnd -= On_Twist;
		EasyTouch.On_PinchEnd -= On_TwistEnd;
	}

	void OnDisable()
	{
		DisableTouch();
	}

	void OnDestroy()
	{
		DisableTouch();
	}

//	public void On_TouchStart(Gesture gesture)
//	{
//		RDebug.Log("gesture  "+gesture.position);
//	}

	void On_SwipeStart(Gesture gesture)
	{
		_cam.TouchHappened();
		RDebug.Log("On_SwipeStart  "+gesture.position);
	}

	void On_Swipe(Gesture gesture)
	{
		_cam.TouchHappened();
		//RDebug.Log("On_Swipe  "+gesture.deltaPosition);
		if(gesture.deltaPosition.x<0) {
			_cam.ChangeRotation();
			_cam.Rotation -= RotateSpeed * Time.deltaTime;
		}
		else if(gesture.deltaPosition.x>0) {
			_cam.ChangeRotation();
			_cam.Rotation += RotateSpeed * Time.deltaTime;
		}
		if(gesture.deltaPosition.y>0) {
			_cam.ChangeTilt();
			float deltaTilt = TiltSpeed * Time.deltaTime;
			float resultTilt = _cam.Tilt - deltaTilt;
			if(resultTilt>=_cam.MinTilt)
				_cam.Tilt -= deltaTilt;
		}
		else if(gesture.deltaPosition.y<0) {
			_cam.ChangeTilt();
			float deltaTilt = TiltSpeed * Time.deltaTime;
			float resultTilt = _cam.Tilt + deltaTilt;
			if(resultTilt<=_cam.MaxTilt)
				_cam.Tilt += deltaTilt;
		}
	}

	void On_SwipeEnd(Gesture gesture)
	{
		RDebug.Log("On_SwipeEnd  "+gesture.position);
	}

	void On_PinchIn(Gesture gesture)
	{
		//RDebug.Log("On_PinchIn  "+gesture.deltaPinch);
		_cam.ChangeZoom();
		float deltaZoom = ZoomSpeed * Time.deltaTime;
		float resultZoom = _cam.Distance - deltaZoom;
		if(resultZoom>= _cam.MinDistance)
			_cam.Distance -= deltaZoom;
	}

	void On_PinchOut(Gesture gesture)
	{
		//RDebug.Log("On_PinchOut  "+gesture.deltaPinch);
		_cam.ChangeZoom();
		float deltaZoom = ZoomSpeed * Time.deltaTime;
		float resultZoom = _cam.Distance + deltaZoom;
		if(resultZoom<= _cam.MaxDistance)
			_cam.Distance += deltaZoom;
	}

	void On_PinchEnd(Gesture gesture)
	{
		RDebug.Log("On_PinchEnd");
	}

	void On_Twist(Gesture gesture)
	{
		//RDebug.Log("On_Twist  "+gesture.twistAngle);
	}
	
	void On_TwistEnd(Gesture gesture)
	{
		//RDebug.Log("On_TwistEnd");
	}
}
